import time
import pytest
import allure
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from faker import Faker
faker = Faker(locale='zh_cn')
phone_number = faker.phone_number()
@allure.feature("存钱罐测试用例")
class TestCQ:
    @pytest.fixture(scope='function',autouse=True)
    def driver(self):
        dict_caps = {}
        dict_caps['platformName'] = 'Android'
        dict_caps['platformVersion'] = '7.1.2'
        dict_caps['deviceName'] = 'x1q'
        dict_caps['appPackage'] = 'com.lingyue.zebraloan'
        dict_caps['appActivity'] = 'com.lingyue.banana.activities.BananaLaunchActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub',dict_caps)
        self.driver.implicitly_wait(15)
        yield
        time.sleep(2)
        self.driver.quit()
    @allure.story("进入用户故事")
    def test_1(self):
        """
        进入存钱罐-点击同意-进入用户故事-滑动页面
        """
        # 进入洋钱罐
        with allure.step("进入洋钱罐") :
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,"com.lingyue.zebraloan:id/tv_confirm"))).click()
        with allure.step('等待主页面加载'):
            time.sleep(6)
        with allure.step("点击用户故事"):
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((MobileBy.ID,"com.lingyue.zebraloan:id/iv_card")))[-1].click()
        with allure.step("等待用户故事页面加载"):
            time.sleep(3)
        with allure.step("滑动页面"):
            """
            开始的位置：x=535、y=1564
            结束的位置：x=541，y=363
            """
            TouchAction(self.driver).press(x=535,y=1564).move_to(x=541,y=363).release().perform()
        with allure.step("断言title：用户故事"):
            title = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,"com.lingyue.zebraloan:id/tv_title"))).text
            assert title == "用户故事"
    @allure.story("登录流程")
    def test_2(self):
        """
        进入存钱罐-点击同意-点击立即借款-输入随机11位手机号码-勾选协议-点击获取短信验证码
        """
        with allure.step("进入洋钱罐") :
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,"com.lingyue.zebraloan:id/tv_confirm"))).click()
        with allure.step('等待主页面加载'):
            time.sleep(6)
        with allure.step("点击立即借款"):
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,"com.lingyue.zebraloan:id/btn_loan"))).click()
        with allure.step("输入随机11位手机号码"):
            text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,"com.lingyue.zebraloan:id/et_mobile_number")))
            # 先清除
            text.clear()
            # 再输入
            text.send_keys(str(phone_number))
        with allure.step("点击同意：协议"):
            time.sleep(2)
            TouchAction(self.driver).tap(x=46,y=862).perform()
        with allure.step("点击：获取验证码按钮"):
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((MobileBy.CLASS_NAME,"android.widget.TextView")))[-1].click()
        with allure.step("断言：手机号码是否发送正确"):
            time.sleep(1)
            text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,"com.lingyue.zebraloan:id/tv_phone"))).text
            assert str(text).strip() == str(phone_number)
            time.sleep(3)